package com.dyh.quickdemozwei.pipe0825;

import java.util.HashMap;

public class PipeLineContext {
    int index;
    HashMap<String,Object> extend;
    Object[] results;

    boolean end;

    public void indexInc(){
        this.index++;
    }

    public PipeLineContext(int size) {
        extend=new HashMap<>();
        results = new Object[size];
    }

    public void setVal(String key, Object obj){
        extend.put(key,obj);
    }


     public <T> T getVal(String key){
        Object raw =  extend.get(key);
         if (raw ==null)
             return null;
         return (T)raw;
    }

//    public Object getVal(String key){
//        return extend.get(key);
//    }
//
//    public <T> T  getVal(String key,Class<T> clazz){
//        Object raw = extend.get(key);
//        if (raw ==null)
//            return null;
//        if (clazz.isAssignableFrom(raw.getClass()))
//            return clazz.cast(raw);
//        return null;
//    }

    public void setResult(Object obj){
        results[index]=obj;
    }

    public <T> T getResult(){
        Object raw = results[results.length-1];

        if (raw ==null)
            return null;
        return (T)raw;
    }

    public void fireEnd(){
//        index = results.length;
        end = true;
    }

    public <T> T getLastResult(){
        Object raw = results[index-1];
        if (raw ==null)
            return null;
        return (T)raw;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}
